<?php 
/*
	Taek User Control
	Contact: Bill Edgar (bill.edgar@oaktondata.com)
	http://www.oaktondata.com/taek-user-control
	
	Copyright (c) 2012, Oakton Data LLC
	All rights reserved.

	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
	    
	    * Redistributions of source code must retain the above copyright
	      notice, this list of conditions and the following disclaimer.
	    * Redistributions in binary form must reproduce the above copyright
	      notice, this list of conditions and the following disclaimer in the
	      documentation and/or other materials provided with the distribution.
	    * Neither the name of the Oakton Data LLC nor the
	      names of its contributors may be used to endorse or promote products
	      derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
	DISCLAIMED. IN NO EVENT SHALL Oakton Data LLC BE LIABLE FOR ANY
	DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
	ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	
	This is the taek_user_control_widget PHP class file.
*/

/* ------------- BEGIN ACTION & FILTER FUNCTIONS -------------- */

/**
 * Method to remove GET variables from URL.
 *
 * @param string $url
 * @return string
 */
function taek_user_control_cleanURI( $url ) {
	if ( $pos = strpos( $url, '?' ) ) {
		$url = substr( $url, 0, $pos );
	}
	return $url;
}

/**
 * Method to set redirect for login fail action.
 * 
 * @param string $username
 */
function taek_user_control_login_fail_action( $username ) {
	$referrer = $_SERVER['HTTP_REFERER']; // get submission source
	// if there�s a valid referrer, and it�s not the default log-in screen
	if ( !empty( $referrer ) && !strstr( $referrer, 'wp-login' ) && !strstr( $referrer, 'wp-admin' ) ) {
		$referrer = taek_user_control_cleanURI( $referrer );
		// append information (login=failed) to the URL for the theme to use
		wp_redirect( $referrer . "?login=failed&username=$username" );
		exit;
	}
}

/**
 *
 * Filter method to set custom login url.
 *
 * @param string $login_url
 * @param string $redirect
 * @return string
 */
function taek_user_control_login_url_filter( $login_url, $redirect ) {
	if ( !empty( $redirect ) ) {
		// first get options array from wordpress database
		$arr = get_option(
			$show = 'widget_taek-user-control-widget',
			$default = false
			);
		// check to see if option array was successfully retrieved
		if ( $arr !== false ) {
			// loop through options array from database
			foreach ( $arr as $instance_id => $properties ) {
				// make sure we're dealing with a valid instance id
				if ( is_numeric( $instance_id ) && count( $properties ) > 1 ) {
					// if custom login url is stored and not empty, return it
					if ( !empty( $properties['customLoginURL'] ) ) {
						return $properties['customLoginURL'];
						// otherwise return default login url
					} else {
						return $login_url;
					}
					// instance id is not valid, or no options are saved, return default login url
				} else {
					return $login_url;
				}
			}
			// options array was not successfully retrieved, return default login url
		} else {
			return $login_url;
		}
		// custom login redirect has already been set for site, return that
	} else {
		return $redirect;
	}
}

/**
 * 
 * Filter method to set custom email address for sending all WP administrative email.
 * 
 * @param string $from_email
 * @return string $from_email
 */
function taek_user_control_mail_from_filter( $from_email ) {
	// first get options array from wordpress database
	$arr = get_option(
		$show = 'widget_taek-user-control-widget',
		$default = false
		);
	// check to see if option array was successfully retrieved
	if ( $arr !== false ) {
		// loop through options array from database
		foreach ( $arr as $instance_id => $properties ) {
			// make sure we're dealing with a valid instance id
			if ( is_numeric( $instance_id ) && count( $properties ) > 1 ) {
				// if mail from address is stored and not empty, return it
				if ( !empty( $properties['mailFromAddress'] ) ) {
					return $properties['mailFromAddress'];
					// otherwise return default mail from
				} else {
					return $from_email;
				}
				// instance id is not valid, or no options are saved, return default login url
			} else {
				return $from_email;
			}
		}
		// options array was not successfully retrieved, return default login url
	} else {
		return $from_email;
	}
}

/**
 * 
 * Filter method to set custom name in from field for all WP administrative email.
 * 
 * @param string $from_name
 * @return string $from_name
 */
function taek_user_control_mail_from_name_filter( $from_name ) {
	// first get options array from wordpress database
	$arr = get_option(
		$show = 'widget_taek-user-control-widget',
		$default = false
		);
	// check to see if option array was successfully retrieved
	if ( $arr !== false ) {
		// loop through options array from database
		foreach ( $arr as $instance_id => $properties ) {
			// make sure we're dealing with a valid instance id
			if ( is_numeric( $instance_id ) && count( $properties ) > 1 ) {
				// if mail from address is stored and not empty, return it
				if ( !empty( $properties['mailFromName'] ) ) {
					return $properties['mailFromName'];
					// otherwise return default mail from
				} else {
					return $from_name;
				}
				// instance id is not valid, or no options are saved, return default login url
			} else {
				return $from_name;
			}
		}
		// options array was not successfully retrieved, return default login url
	} else {
		return $from_name;
	}
}

/**
 *
 * Filter method to check for username or email exists when registering a new user and set redirect.
 *
 * @param object $errors
 * @param string $login
 * @param string $email
 */
function taek_user_control_registration_errors_filter( $registerErrors, $login, $email ) {
	$referrer = $_SERVER['HTTP_REFERER']; // get submission source
	// if there�s a valid referrer, and it�s not the default log-in screen
	if ( !empty( $referrer ) && is_object( $registerErrors ) ) {
		if ( isset( $registerErrors->errors['email_exists'] ) ) {
			$referrer = taek_user_control_cleanURI( $referrer );
			// append information (register=false&error=email_exists) to the URL for the theme to use
			wp_redirect( $referrer . '?register=false&register_error=email_exists' );
		} elseif ( isset( $registerErrors->errors['username_exists'] ) ) {
			$referrer = taek_user_control_cleanURI( $referrer );
			// append information (register=false&error=username_exists) to the URL for the theme to use
			wp_redirect( $referrer . '?register=false&register_error=username_exists' );
		}
	}
	return $registerErrors;
} // end function registration_errors_filter

/* ------------- END ACTION & FILTER FUNCTIONS -------------- */

if ( !class_exists( 'taek_user_control_Widget' ) ) {
	
	// taek_user_control_widget class
	class taek_user_control_Widget extends taek_SidebarWidget {
		
		private $tabSelections = array();
			
		/* -------------------------------------------------------------------------*/
		/* Constructor
		/* -------------------------------------------------------------------------*/
		
		/**
		 * Widget constructor method. Must be overridden in child class.
		 */
		function taek_user_control_Widget() {
			// initialize widget
			$my_locale = ( get_locale() ) ? get_locale() : 'en_US';
			$this->init( array(
				'widgetDescription' =>	__( 'Customizable user control widget for login, registration, lost password, etc. within sidebar.', 'taek-user-control' ),
				'widgetDomain' =>		'taek-user-control',
				'widgetLocale' => 		$my_locale,
				'widgetName' =>			'Taek User Control Widget',
				'widgetSlug' =>			'taek-user-control-widget',
				'widgetStyle' =>		TAEK_USER_CONTROL_WIDGET_STYLE,
				'widgetStyleHandle' => 	'taek-user-control-widget-style'
				) );
			// add action hooks
			add_action(
				$tag = 'taek_login_failed',
				$function_to_add = 'taek_user_control_login_fail_action',
				$priority = 10,
				$accepted_args = 1
				); // hook failed login
			
			// add filter hooks
			add_filter(
				$tag = 'login_url',
				$function_to_add = 'taek_user_control_login_url_filter',
				$priority = 10,
				$accepted_args = 2
				); // hook login url
			
			 add_filter(
		 		$tag = 'registration_errors',
		 		$function_to_add = 'taek_user_control_registration_errors_filter',
		 		$priority = 10,
		 		$accepted_args = 3
			 	); // hook failed registration

			 add_filter(
			 	$tag = 'taek_mail_from',
			 	$function_to_add = 'taek_user_control_mail_from_filter',
			 	$priority = 10,
			 	$accepted_args = 1
			 	); // hook mail from address
			 
			 add_filter(
		 		$tag = 'taek_mail_from_name',
		 		$function_to_add = 'taek_user_control_mail_from_name_filter',
		 		$priority = 10,
		 		$accepted_args = 1
			 	); // hook mail from name
			
		} // end constructor method
		
		/* -------------------------------------------------------------------------*/
		/* Private Functions
		/* -------------------------------------------------------------------------*/
		
	    /**
	    *
	    * method to set up default options for widget. Must be overidden by child class.
	    */
	    protected function setUpDefaults() {
	    	$this->defaults = array(
	    		'customLink' =>				'',
	    		'customLinkTitle' =>		'',
	    		'customLoginURL' =>			'',
	    		'defaultTab' =>				'login',
	    		'displayAvatar' =>			'enabled',
	    		'loginButtonLabel' =>		__( 'Login', 'taek-user-control' ),
	    		'loginTabLabel' =>			__( 'Login', 'taek-user-control' ),
	    		'mailFromAddress' =>		get_option( 'admin_email' ),
	    		'mailFromName' =>			get_option( 'blogname' ),
	    		'resetTabLabel' =>			__( 'Reset', 'taek-user-control' ),
	    		'resetButtonLabel' =>		__( 'Reset Password', 'taek-user-control' ),
	    		'registerButtonLabel' =>	__( 'Register', 'taek-user-control' ),
	    		'registerTabLabel' =>		__( 'Register', 'taek-user-control' ),
	    		'title' =>					__( 'Taek User Control Widget', 'taek-user-control' )
	    		);
	    	$this->tabSelections = array(
    			'login' =>		__( 'Login', 'taek-user-control' ),
    			'register' => 	__( 'Register', 'taek-user-control' ),
    			'reset' => 		__( 'Reset', 'taek-user-control' )
	    		);
	    }
		
		/* -------------------------------------------------------------------------*/
		/* WP_Widget API Functions
		/* -------------------------------------------------------------------------*/
	    
		/** 
	     * Outputs widget settings form. Overrides WP_Widget::form().  Must be overidden by child class.
	     * 
	     * @param object $instance	array of widget options. 
	     */
		public function form( $instance ) {
			
			// merge instance options with defaults
			$instance = wp_parse_args( ( array ) $instance, $this->defaults );
			
			// output title text field
			$this->outputFormField( array( 
				'class' => 			'widefat',
				'label' =>			__( 'Title ', 'taek-user-control' ),
				'field' => 			'title',
				'type' => 			'text',
				'value' => 			$instance['title']
				) );
			// output fill background checkbox
			$this->outputFormField( array(
				'label' =>			__( 'Display Avatar ', 'taek-user-control' ), 
				'field' =>			'displayAvatar',
				'type' => 			'checkbox',
				'value' => 			$instance['displayAvatar']
				) );
			// output login tab label field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Login Tab Label ', 'taek-user-control' ),
				'field' => 			'loginTabLabel',
				'type' => 			'text',
				'value' => 			$instance['loginTabLabel']
				) );
			// output login button label field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Login Button Label ', 'taek-user-control' ),
				'field' => 			'loginButtonLabel',
				'type' => 			'text',
				'value' => 			$instance['loginButtonLabel']
				) );
			// output reset tab label field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Reset Tab Label ', 'taek-user-control' ),
				'field' => 			'resetTabLabel',
				'type' => 			'text',
				'value' => 			$instance['resetTabLabel']
				) );
			// output reset button label field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Reset Button Label ', 'taek-user-control' ),
				'field' => 			'resetButtonLabel',
				'type' => 			'text',
				'value' => 			$instance['resetButtonLabel']
				) );
			// output register tab label field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Register Tab Label ', 'taek-user-control' ),
				'field' => 			'registerTabLabel',
				'type' => 			'text',
				'value' => 			$instance['registerTabLabel']
				) );
			// output register button label field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Register Button Label ', 'taek-user-control' ),
				'field' => 			'registerButtonLabel',
				'type' => 			'text',
				'value' => 			$instance['registerButtonLabel']
				) );
			// output default tab dropdown
			$this->outputFormField( array(
				'label' =>			__( 'Default Tab ', 'taek-user-control' ),
				'field' =>			'defaultTab',
				'my_selection' => 	$instance['defaultTab'],
				'selections' =>		$this->tabSelections,
				'style' =>			'width:80px',
				'type' => 			'dropdown'
				) );
			// output custom login url field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Custom Login URL ', 'taek-user-control' ),
				'field' => 			'customLoginURL',
				'type' => 			'text',
				'value' => 			$instance['customLoginURL']
				) );
			// output custom login url field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Custom Link Title', 'taek-user-control' ),
				'field' => 			'customLinkTitle',
				'type' => 			'text',
				'value' => 			$instance['customLinkTitle']
				) );
			// output custom login url field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'Custom Link ', 'taek-user-control' ),
				'field' => 			'customLink',
				'type' => 			'text',
				'value' => 			$instance['customLink']
				) );
			// output custom email address field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'From Email Address ', 'taek-user-control' ),
				'field' => 			'mailFromAddress',
				'type' => 			'text',
				'value' => 			$instance['mailFromAddress']
				) );
			// output custom email address field
			$this->outputFormField( array(
				'class' => 			'widefat',
				'label' =>			__( 'From Name ', 'taek-user-control' ),
				'field' => 			'mailFromName',
				'type' => 			'text',
				'value' => 			$instance['mailFromName']
				) );
		}
		
		/** 
	     * Processes widget options to be saved. Overrides WP_Widget::update().  Must be overidden by child class.
	     * 
	     * @param object $new_instance	previous instance values before update 
	     * @param object $old_instance  new instance values to be saved via update 
	     * @return object $instance
	     */ 
		public function update( $new_instance, $old_instance ) {
			// set instance to point to old options array
			$instance = $old_instance;
			
			// record new instance values for option keys
			$instance['customLink'] = trim( strip_tags( $new_instance['customLink'] ) );
			$this->OPTIONS[$this->number]['customLink'] = $instance['customLink'];
			$instance['customLinkTitle'] = trim( strip_tags( $new_instance['customLinkTitle'] ) );
			$this->OPTIONS[$this->number]['customLinkTitle'] = $instance['customLinkTitle'];
			$instance['customLoginURL'] = trim( strip_tags( $new_instance['customLoginURL'] ) );
			$this->OPTIONS[$this->number]['customLoginURL'] = $instance['customLoginURL'];
			$instance['defaultTab'] = trim( strip_tags( $new_instance['defaultTab'] ) );
			$this->OPTIONS[$this->number]['defaultTab'] = $instance['defaultTab'];
			$instance['displayAvatar'] = ( $new_instance['displayAvatar'] == 'enabled' ) ? trim( strip_tags( $new_instance['displayAvatar'] ) ) : 'disabled';
			$this->OPTIONS[$this->number]['displayAvatar'] = $instance['displayAvatar'];
			$instance['loginButtonLabel'] = trim( strip_tags( $new_instance['loginButtonLabel'] ) );
			$this->OPTIONS[$this->number]['loginButtonLabel'] = $instance['loginButtonLabel'];
			$instance['loginTabLabel'] = trim( strip_tags( $new_instance['loginTabLabel'] ) );
			$this->OPTIONS[$this->number]['loginTabLabel'] = $instance['loginTabLabel'];
			$instance['mailFromAddress'] = trim( strip_tags( $new_instance['mailFromAddress'] ) );
			$this->OPTIONS[$this->number]['mailFromAddress'] = $instance['mailFromAddress'];
			$instance['mailFromName'] = trim( strip_tags( $new_instance['mailFromName'] ) );
			$this->OPTIONS[$this->number]['mailFromName'] = $instance['mailFromName'];			
			$instance['registerButtonLabel'] = trim( strip_tags( $new_instance['registerButtonLabel'] ) );
			$this->OPTIONS[$this->number]['registerButtonLabel'] = $instance['registerButtonLabel'];
			$instance['registerTabLabel'] = trim( strip_tags( $new_instance['registerTabLabel'] ) );
			$this->OPTIONS[$this->number]['registerTabLabel'] = $instance['registerTabLabel'];
			$instance['resetButtonLabel'] = trim( strip_tags( $new_instance['resetButtonLabel'] ) );
			$this->OPTIONS[$this->number]['resetButtonLabel'] = $instance['resetButtonLabel'];
			$instance['resetTabLabel'] = trim( strip_tags( $new_instance['resetTabLabel'] ) );
			$this->OPTIONS[$this->number]['resetTabLabel'] = $instance['resetTabLabel'];
			$instance['title'] = trim( strip_tags( $new_instance['title'] ) );
			$this->OPTIONS[$this->number]['title'] = $instance['title'];
			
			// return updated options array
			return $instance;
		}
		
		/** 
	     * Outputs widget content. Overrides WP_Widget::widget(). Must be overidden by child class.
	     * 
	     * @param array $args		array of form elements 
	     * @param object $instance	widget instance
	     */
		public function widget( $args, $instance ) {
			$username = null;
			extract( $args );
			// output custom WP widget wrapper
			echo $before_widget;
			// output title based on option input
			$title = apply_filters( 'widget_title', $instance['title'] );
			$customLink = empty( $instance['customLink'] ) ? $this->defaults['customLink'] : $instance['customLink'];
			$customLinkTitle = empty( $instance['customLinkTitle'] ) ? $this->defaults['customLinkTitle'] : $instance['customLinkTitle'];
			$default_tab = empty( $instance['defaultTab'] ) ? $this->defaults['defaultTab'] : $instance['defaultTab'];
			$displayAvatar = empty( $instance['displayAvatar'] ) ? $this->defaults['displayAvatar'] : $instance['displayAvatar'];
			$loginButtonLabel = empty( $instance['loginButtonLabel'] ) ? $this->defaults['loginButtonLabel'] : $instance['loginButtonLabel'];
			$loginTabLabel = empty( $instance['loginTabLabel'] ) ? $this->defaults['loginTabLabel'] : $instance['loginTabLabel'];
			$registerButtonLabel = empty( $instance['registerButtonLabel'] ) ? $this->defaults['registerButtonLabel'] : $instance['registerButtonLabel'];
			$registerTabLabel = empty( $instance['registerTabLabel'] ) ? $this->defaults['registerTabLabel'] : $instance['registerTabLabel'];
			$resetButtonLabel = empty( $instance['resetButtonLabel'] ) ? $this->defaults['resetButtonLabel'] : $instance['resetButtonLabel'];
			$resetTabLabel = empty( $instance['resetTabLabel'] ) ? $this->defaults['resetTabLabel'] : $instance['resetTabLabel'];
			// set default active tab
			$active_tab = $default_tab;
		    // output widget title with WP wrapper
		    if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
		    // output html
		    try {
?>


                <div id="login-register-password">

<?php 
    						global $user_ID, $user_identity; 
    						get_currentuserinfo();
    						global $user_login, $user_email;
    						if ( !$user_ID ) { 
    							// grab POST variables
    							$login = ( array_key_exists( 'login', $_GET ) ) ? trim( $_GET['login'] ) : false;
    							$register = ( array_key_exists( 'register', $_GET ) ) ? trim( $_GET['register'] ) : false;
    							$reset = ( array_key_exists( 'reset', $_GET ) ) ? trim( $_GET['reset'] ) : false;
?>
<?php // Output tabs ?>
                  <ul class="tabs_login">
                    <li id="login_tab"><a href="#login_div"><?php echo $loginTabLabel; ?></a></li>
                  </ul>

                  <div class="tab_container_login">
                    
<?php // LOGIN FORM BEGIN ?>

                    <div id="login_div" class="tab_content_login" style="display:none;">
    								<?php 
    								if ( $login == 'failed' ) { 
    									$username = $_GET['username']; 
    									$active_tab = 'login';
    								?>

    									<p class="uc-message-fail">
    									<?php _e( 'Login failed. Please check your username and password.', 'taek-user-control' ); ?>
    									</p>
    								
    								<?php // custom password reset function to handle checking for valid username or email
    								} else { ?>
    									<p>
    									<?php _e( 'Enter your username and password below to login.', 'taek-user-control' ); ?>
    									</p><?php 
    								} ?>
    								<form method="post" action="<?php bloginfo( 'url' ) ?>/wp-login.php" class="wp-user-form">
    									<div class="username">
    										<label for="user_login"><?php _e( 'Username', 'taek-user-control' ); ?>: </label>
    										<input type="text" name="log" value="<?php 
		    									if ( !isset( $username ) ) {
		    										echo trim( stripslashes( $user_login ) );
		    									} else {
		    										echo trim( stripslashes( $username ) );
		    									}  								
    										?>" id="user_login" tabindex="11" />
    									</div>
    									<div class="password">
    										<label for="user_pass"><?php _e( 'Password', 'taek-user-control' ); ?>: </label>
    										<input type="password" name="pwd" value="" id="user_pass" tabindex="12" />
    									</div>
    									<div class="login_fields">
    										<div class="rememberme">
    											<label for="rememberme">
    												<input type="checkbox" name="rememberme" value="forever" checked="checked" id="rememberme" tabindex="13" /><?php _e( 'Remember me', 'taek-user-control' ); ?>
    											</label>
    										</div>
    										<?php do_action( 'login_form' ); ?>
    										<input type="submit" name="user-submit" value="<?php echo $loginButtonLabel; ?>" tabindex="14" class="user-submit" />
    										<input type="hidden" name="redirect_to" value="<?php echo taek_user_control_cleanURI( $_SERVER['REQUEST_URI'] ); ?>" />
    										<input type="hidden" name="user-cookie" value="1" />
    									</div>
    								</form>
                      
                    <ul class="tabs_login_extra">
                      <li id="register_tab"><a href="#register_div"><?php echo $registerTabLabel; ?></a></li>
                      <li id="reset_tab"><a href="#reset_div"><?php echo $resetTabLabel; ?></a></li>
                    </ul>
                      
    							</div>
                      
<?php 
// LOGIN FORM END
// REGISTRATION FORM BEGIN 
?>
    							<div id="register_div" class="tab_content_login_extra" style="display:none;">
    								<?php 
    								$register_user = $register_email = null;
    								// if new user registration was submitted...
    								if ( $register == 'true' ) { 
    									$active_tab = 'register'; 
    								?>
    									<p class="uc-message"><?php _e( 'Check your email for the password and then return to log in.', 'taek-user-control' ); ?></p> <?php 
    								} elseif ( $register == 'false' ) {	
    									$active_tab = 'register';
    									$registerError = ( array_key_exists( 'register_error', $_GET ) ) ? $_GET['register_error'] : false;
    									if ( $registerError == 'username_exists' ) { ?>
											<p class="uc-message-fail">
											<?php 
												_e( 'Registration failed. Username exists. Please choose another.', 'taek-user-control' ); 
												$register_user = ( array_key_exists( 'user_login', $_POST ) ) ? $_POST['user_login'] : null;
											?>
											</p><?php 
    									} elseif ( $registerError == 'email_exists' ) { ?>
    									    <p class="uc-message-fail">
    									    <?php 
    									    	_e( 'Registration failed. Email already registered. Did you forget your password?', 'taek-user-control' );
    									    	$register_email = ( array_key_exists( 'user_email', $_POST ) ) ? $_POST['user_email'] : null; 
    									    ?>
    									    </p><?php 	
    									} else { ?>				
    									    <p class="uc-message-fail">
    									    <?php _e( 'Registration failed. Unknown error.', 'taek-user-control' ); ?>
    									    </p><?php
    									}
    								} else { ?>
    									<p><?php _e( 'Complete the form below to register.', 'taek-user-control' ); ?></p><?php 
    								} ?>
    								<form method="post" action="<?php echo site_url( 'wp-login.php?action=register', 'login_post' ) ?>" class="wp-user-form">
    									<div class="username">
    										<label for="user_login"><?php _e( 'Username', 'taek-user-control' ); ?>: </label>
    										<input type="text" name="user_login" value="<?php 
    												if ( !empty( $register_user ) ) {
    													echo stripslashes( $register_user );
    												} else {
    													echo stripslashes( $user_login ); 
    												} 
    											?>" id="user_login" tabindex="101" />
    									</div>
    									<div class="password">
    										<label for="user_email"><?php _e( 'Email', 'taek-user-control' ); ?>: </label>
    										<input type="text" name="user_email" value="<?php 
    												if ( !empty( $register_email ) ) {
    													echo stripslashes( $register_email );
    												} else {
    													echo stripslashes( $user_email ); 
    												}
    											?>" id="user_email" tabindex="102" />
    									</div>
    									<div class="login_fields">
    										<?php do_action( 'register_form' ); ?>
    										<input type="submit" name="user-submit" value="<?php echo $registerButtonLabel; ?>" class="user-submit" tabindex="103" />
    										<input type="hidden" name="redirect_to" value="<?php echo taek_user_control_cleanURI( $_SERVER['REQUEST_URI'] ); ?>?register=true" />
    										<input type="hidden" name="user-cookie" value="1" />
    									</div>
    								</form>
    							</div>
<?php
// REGISTRATION FORM END
// RESET FORM BEGIN
?>
    							<div id="reset_div" class="tab_content_login_extra" style="display:none;"><?php
    								
    								if ( $reset == 'true' ) {
    									$active_tab = 'reset';
    									global $wpdb;
    									$reset_username = ( array_key_exists( 'user_login', $_POST ) ) ? trim( $_POST['user_login'] ) : null;
    									$user_exists = false;
    									if ( !empty( $reset_username ) ) {
	    									// check for username
	    									if ( username_exists( $reset_username ) ) {
	    										$user_exists = true;
	    										$reset_user = get_user_by( 'login', $reset_username );
	    									// check for email
	    									} elseif ( email_exists( $username ) ) {
	    										$user_exists = true;
	    										$reset_user = get_user_by( 'email', $reset_username );
	    									// otherwise, user does not exist
	    									} else {
	    										$error[] = '<p class="uc-message-fail">' . __( 'Username or email does not exist.', 'taek-user-control' ) . '</p>';
	    										$reset = false;
	    									}
    									} else {
    										$error[] = '<p class="uc-message-fail">' . __( 'Invalid username or email. Please try again.', 'taek-user-control' ) . '</p>';
    									}
    									// if user exists, then proceed
    									if ( $user_exists ) {
    										$reset_user_login = $reset_user->user_login;
    										$reset_user_email = $reset_user->user_email;
    										// Generate something random for a password... md5'ing current time with a rand salt
    										$key = substr( md5( uniqid( microtime() ) ), 0, 8);
    										// Now insert the new pass md5'd into the WP DB
    										$wpdb->query("UPDATE $wpdb->users SET user_activation_key = '$key' WHERE user_login = '$reset_user_login'" );
    										// create password reset email message
    										$message = __('Someone has asked to reset the password for the following site and username.', 'taek-user-control' ) . "\r\n\r\n";
    										$message .= network_site_url() . "\r\n\r\n";
    										// $message .= get_option( 'siteurl' ) . "\r\n\r\n";
    										$message .= sprintf( __( 'Username: %s', 'taek-user-control' ), $reset_user_login ) . "\r\n\r\n";
    										$message .= __( 'To reset your password visit the following address, otherwise just ignore this email and nothing will happen.', 'taek-user-control' ) . "\r\n\r\n";
    										$message .= network_site_url( "wp-login.php?action=rp&key=" . rawurlencode( $key ) . "&login=" . rawurlencode( $reset_user_login ), 'login' ) . "\r\n";
    										// $message .= get_option( 'siteurl' ) . "/wp-login.php?action=rp&key=$key\r\n";
    										// send password reset email meassage
    										if ( false == wp_mail( $reset_user_email, sprintf( __( '[%s] Password Reset', 'taek-user-control' ), get_option( 'blogname' ) ), $message ) ) {
    											$error[] = '<p class="uc-message-fail">' . __( 'E-mail could not be sent.', 'taek-user-control' ) . "<br />\n" 
    												. __( 'Possible reason: your host may have disabled the mail() function...', 'taek-user-control' ) . '</p>';
    										}
    									}
    									// output errors, if appropriate
    									if ( count( $error ) > 0 ) {
    										foreach ( $error as $e ) {
    											echo $e;
    										}
    										$reset = false;
    									// otherwise password reset was successful, so output message
    									} else { ?>
    										<p class="uc-message"><?php _e( 'Check your email to reset your password.', 'taek-user-control' ); ?></p><?php 
    									}
    								} else { ?>
    									<p><?php _e( 'Enter your username or email to reset your password.', 'taek-user-control' ); ?></p><?php 
    								} ?>
    								<form method="post" action="<?php echo taek_user_control_cleanURI( $_SERVER['REQUEST_URI'] ); ?>?reset=true" class="wp-user-form">
    									<div class="username">
    										<label for="user_login" class="hide"><?php _e( 'Username or Email', 'taek-user-control' ); ?>: </label>
    										<input type="text" name="user_login" value="<?php 
    											if ( !empty( $reset_username ) ) {
    												echo $reset_username;
    											}
    										?>" id="user_login" tabindex="1001" />
    									</div>
    									<div class="login_fields">
    										<?php do_action( 'login_form', 'resetpass' ); ?>
    										<input type="submit" name="user-submit" value="<?php echo $resetButtonLabel; ?>" class="user-submit" tabindex="1002" />
    										<input type="hidden" name="user-cookie" value="1" />
    									</div>
    								</form>
    							</div>
    						</div>

<?php // RESET FORM END ?>

<?php
// LOGGED IN USER BEGIN
                } else { // is logged in 
                  
                  global $userdata; get_currentuserinfo();
?>
                  <div class="sidebox">

                    <div class="userinfo">
                      <?php echo __( 'Welcome,', 'taek-user-control' ) . ' ' . $user_identity; ?> - <a href="<?php echo wp_logout_url( taek_user_control_cleanURI( $_SERVER['REQUEST_URI'] ) ) ; ?>"><?php _e( 'Log out', 'taek-user-control' ); ?></a>
                    </div>
                  <?php // output custom link, if desired
                  if (!empty($customLink) && !empty($customLinkTitle)) { ?>

                    <div id="taek-uc-custom-link">
                      <a href="<?php echo $customLink; ?>"><?php echo $customLinkTitle; ?></a>
                    </div>
                  <?php } ?>

                  </div>

                <?php } ?>

                </div>
    					<!-- Taek User Control Widget JS -->
    					<script type="text/javascript" charset="utf-8">
							taek_user_control_widget_js();
<?php if (isset($_GET['reset'])) { ?>
              open_user_control_box('reset');
<?php } ?>
<?php if (isset($_GET['register'])) { ?>
              open_user_control_box('register');
<?php } ?>
    					</script>
    					<!-- Taek User Control Widget JS -->
    				<?php 
				
		    } catch ( wp_PluginException $e ) {
		    	echo $e->getError();
		    }
			// output custom WP widget wrapper
			echo $after_widget;
		}
	}
}

// hook into widgets_init action
if ( class_exists( 'taek_user_control_widget' ) ) {
	add_action('widgets_init', create_function('', 'register_widget("taek_user_control_widget");'));
}
?>